<!--
 * @Description: 文件描述,
 * @Version: 1.0
 * @Author: YueXuanzi
 * @Date: 2024-09-08 17:43:33
 * @LastEditors: YueXuanzi
 * @LastEditTime: 2024-10-08 08:47:41
 * @心得体会: 无
-->
<template>
    <div class="w-full">
        <el-form :inline="true" :model="formInline" class="w-full h-10">
            <el-form-item label="医生姓名">
                <el-input v-model="formInline.doctorName" placeholder="医生姓名" clearable class="w-40"/>
            </el-form-item>
            <el-form-item label="医生电话">
                <el-input v-model="formInline.phonenumber" placeholder="医生电话" clearable class="w-40"/>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="onSearch">搜索</el-button>
            </el-form-item>
        </el-form>
    </div>
    <!-- 表格显示 -->
    <el-table :data="tableData" style="width: 100%" border height="60vh" highlight-current-row @row-dblclick="getInfo">
        <!-- <el-table-column fixed type="selection" width="50" /> -->
        <el-table-column prop="doctorId" label="序号" width="80" align="center"></el-table-column>
        <el-table-column label="图片"  align="center" width="80">
            <template v-slot="scope">
                <el-image class="h-10" :src="scope.row.doctorImg">
                </el-image>
            </template>
        </el-table-column>
        <el-table-column prop="doctorname" label="医生姓名"  align="center"></el-table-column>
        <el-table-column prop="sex" label="性别"  align="center">
            <template v-slot="scope">
                <!-- 标签 -->
                <el-tag v-if="scope.row.sex == 1" type="success">男</el-tag>
                <el-tag v-else type="primary">女</el-tag>
            </template>
        </el-table-column>
        <el-table-column prop="profile" label="简介"  align="center"></el-table-column>
        <el-table-column prop="phonenumber" label="电话"  align="center"></el-table-column>
        <el-table-column prop="email" label="邮箱"  align="center"></el-table-column>
        <el-table-column prop="registerfee" label="预约费用"  align="center"></el-table-column>
        <el-table-column label="操作"  align="center">
            <template #default="scope">
                <el-button type="primary" @click="toRegister(scope.row)">进行预约</el-button>
            </template>
        </el-table-column>
    </el-table>
    <!-- 页码 -->
    <div class="flex justify-center items-end w-full h-16 mt-2">
        <el-pagination
            v-model:current-page="pageForm.currentPage"
            v-model:page-size="pageForm.pageSize"
            :page-sizes="[10, 20, 30, 40]"
            :background="background"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageForm.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            />
            <el-switch v-model="background" class="ml-2" />
    </div>
    <!-- 弹出对话框 -->
    <el-dialog v-model="dialogVisible" title="预约中...." width="400" :before-close="handleClose">
        <el-form :model="registerForm" label-width="auto" class="w-full">
            <el-form-item label="医生姓名">
                <el-text class="w-40">{{ registerForm.doctorname }}</el-text>
            </el-form-item>
            <el-form-item label="预约费用">
                <el-text class="w-40">{{ registerForm.registerfee }}</el-text>
            </el-form-item>
            <el-form-item label="支付方式">
                <el-select
                v-model="registerForm.method"
                placeholder="选择支付方式"
                clearable
                class="w-60">
                    <el-option value="0" label="微信支付">微信支付</el-option>
                    <el-option value="1" label="支付宝支付">支付宝支付</el-option>
                </el-select>
            </el-form-item>
            <!-- 这里有一个支付的图片，需要支付模块 -->
            当下支持零元购！！！
            <el-form-item>
                <el-button @click="handleClose">取消</el-button>
                <el-button type="primary" @click="registerCome">
                    确定预约
                </el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
    <!-- 双击查看详情 -->
    <el-dialog v-model="dialogInfoVisible" title="医生详情" width="400">
        <el-form :model="doctorInfo" label-width="auto" class="w-full">
            <el-form-item label="医生姓名">
                <el-text class="w-40">{{ doctorInfo.doctorname }}</el-text>
            </el-form-item>
            <el-form-item label="性别">
                <el-text class="w-40" v-if="doctorInfo.sex == 1">男</el-text>
                <el-text class="w-40" v-else>女</el-text>
            </el-form-item>
            <el-form-item label="邮箱">
                <el-text class="w-40">{{ doctorInfo.email }}</el-text>
            </el-form-item>
            <el-form-item label="电话">
                <el-text class="w-40">{{ doctorInfo.phonenumber }}</el-text>
            </el-form-item>
            <el-form-item label="简介">
                <el-text class="w-40">{{ doctorInfo.profile }}</el-text>
            </el-form-item>
            <el-form-item label="预约费用">
                <el-text class="w-40 text-green-400">{{ doctorInfo.registerfee }}</el-text>
            </el-form-item>
        </el-form>
    </el-dialog>
</template>

<script setup>
import { computed, ref, reactive } from 'vue'
import { ElMessageBox, ElMessage } from 'element-plus'
import { getDoctorList, toRegisterX } from "@/api/patient/reservation.js"
// 表格数据
const tableData = ref([
    {id: 1, doctorImg: "@/assets/img/xx.png", doctorName: "张三", sex: "男", description: "我是一个好医生", phonenumber: "123456789", price: "100", status: 1},
    {id: 2, doctorImg: "@/assets/img/xx.png", doctorName: "张三", sex: "男", description: "我是一个好医生", phonenumber: "123456789", price: "100", status: 1},
    {id: 3, doctorImg: "@/assets/img/xx.png", doctorName: "张三", sex: "男", description: "我是一个好医生", phonenumber: "123456789", price: "100", status: 1}
])
// 对话框可见
let dialogVisible = ref(false)
let dialogInfoVisible = ref(false)
// 背景
const background = ref(false)
// 最后一页面
const pageForm = reactive({
    currentPage: 1,
    pageSize: 10,
    total: 50
})
const formInline = reactive({
  doctorName: "",
  phonenumber : ""
})
// 引入图片
const src= new URL('../../assets/img/no.png', import.meta.url).href
// 搜素数据
const pageSearch = computed(() => {
  return {
    page: pageForm.currentPage,
    limit: pageForm.pageSize,
    doctorname: formInline.doctorName,
    phonenumber: formInline.phonenumber
  }
})
// 预约表单
const registerForm = reactive({
  doctorId: "",
  doctorname: "",
  time: "",
  register: "",
  method: ""
})
// 医生详情页
let doctorInfo = reactive({})
// 初始化数据
initData()
// 初始化方法
function initData(){
    console.log(pageSearch.value.limit)
    getDoctorList(pageSearch.value).then(res =>{
        console.log(res)
        // 初始化表格数据
        tableData.value = res.data.pageList.list
        pageForm.total = Number(res.data.pageList.totalCount)
        pageForm.currentPage = res.data.pageList.currPage
        pageForm.pageSize = res.data.pageList.pageSize
    })
}

// 搜搜
const onSearch = () => {
  initData()
}
// 双击查看
const getInfo = (row) => {
//   console.log(row)
  doctorInfo = row
  dialogInfoVisible.value = true
//   console.log(doctorInfo)
}
// 进行预约
const toRegister = (row) => {
  // 设置可见
  dialogVisible.value = true
  registerForm.doctorId = row.doctorId
  registerForm.doctorname = row.doctorname
  registerForm.registerfee = row.registerfee
//   console.log(registerForm)
}
const handleClose = () => {
  ElMessageBox.confirm('你确定取消当前预约吗？',{
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    })
    .then(() => {
        dialogVisible.value = false
        ElMessage({
            type: 'success',
            message: "已取消",
        })
    })
    .catch(() => {
      
    })
}
// 确定预约
const registerCome = () => {
  console.log("确定预约")
  const RegisterX = {
    doctorId : registerForm.doctorId,
    timeSlotId: 1,
    method: registerForm.method,
    price : registerForm.registerfee
  }
  toRegisterX(RegisterX).then(res =>{
    console.log(res)
    if(res.code == 200){
        ElMessage({
            type: 'success',
            message: "预约成功",
        })
    }else{
        ElMessage({
            type: 'error',
            message: res.msg,
        })
    }
  })
  dialogVisible.value = false
  
}

const handleSizeChange = (val) => {
  // console.log(`${val} items per page`)
  initData()
}
const handleCurrentChange = (val) => {
  // console.log(`current page: ${val}`)
  initData()
}
</script>

<style scoped>

</style>